
<?

 function display_stage_edit($idp, $ids, $idss)
 {
$nume="";
 if ($idss!=0)
 {
$resultsub=returnQuery("select * from substages where id='".$idss."';", $dbh);
$rowsub=mysql_fetch_assoc($resultsub);
$nume=$rowsub['substage_name'];
}
else
{
$resultst=returnQuery("select * from stages where id='".$ids."';", $dbh);
$rowst=mysql_fetch_assoc($resultst);
$nume=$rowst['stage_name'];
}

 
 ?>
 
	<form action="" name="edit_form" method="POST">
			
			<table width="90%">
				<tr><td width="100px">Nume</td><td><input name="sname" type="text" value="<? echo $nume; ?>" style="width:200px"></td></tr>
				<tr><td><input type="submit" name="qr_update_st" value="Update" ></td>
			<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
			</table>
			</form>
			
			
	<?
 }


 function display_doc_edit($idp, $idd)
 {
 
$resultdoc = returnQuery("select * from documents where id = ".$idd.";",'');
$rowdoc = mysql_fetch_assoc($resultdoc);

if($rowdoc['id_stage']==0)		
	{	

		$i = 0; 
			$arrStages = array(); 
			$arrStagesID = array();
			
			$instructiune="SELECT stage_name, id FROM stages WHERE id_project=".$idp." order by stage_order asc;";
			$interogare=mysql_query($instructiune);
			while($row=mysql_fetch_array($interogare))
			{
				$arrStages[$i] = $row["stage_name"]; 
				$arrStagesID[$i] = $row["id"];
				$i++; 
			}
			
			$j = 0;
			$arrSubstagesP=array();
			$arrSubstages = array(); 
			$arrSubstagesID = array();
			
			$instruct="SELECT substage_name, id, id_stage FROM substages WHERE id_stage in( select id from stages where id_project=".$idp.") order by substage_order asc;";
			$interog=mysql_query($instruct);
			while($row=mysql_fetch_array($interog))
			{
				$arrSubstages[$j] = $row["substage_name"]; 
				$arrSubstagesID[$j] = $row["id"];
				$arrSubstagesP[$j]=$row["id_stage"];
				$j++; 
				//echo $j;
			}
			?>
			<script type="text/javascript"> 
			
			/*	var docid;
				var idpr;
				var idst;
				var idss;
				var numed;
				var acc;
				<? echo "docid='".$rowdoc['id']."';\n"; 
				echo "idpr='".$rowdoc['id_project']."';\n"; 
				echo "idst='".$rowdoc['id_stage']."';\n"; 
				echo "idss='".$rowdoc['id_substage']."';\n";
				echo "numed='".$rowdoc['name']."';\n";  
				echo "acc='".$rowdoc['for_acceptance']."';\n"; 
				?>
			*/
				arrStage = new Array(); 
				arrStageID = new Array();
				<?php 
				for($i=0;$i<count($arrStages); $i++){ 
					echo "arrStage[$i]='".$arrStages[$i]."';\n"; 
					echo "arrStageID[$i]='".$arrStagesID[$i]."';\n";
				} 
				?>  
				arrSubstage = new Array(); 
				arrSubstageID = new Array(); 
				arrSubstageP = new Array();
				<?php 
				for($i=0;$i<count($arrSubstages); $i++){ 
					echo "arrSubstage[$i]='".$arrSubstages[$i]."';\n"; 
					echo "arrSubstageID[$i]='".$arrSubstagesID[$i]."';\n"; 
					echo "arrSubstageP[$i]='".$arrSubstagesP[$i]."';\n"; 
				} 
				?> 
				

			function umple(idx) 
				{
				var f=document.doc_edit;
				f.sel_place.options.length=null;
				if (idx==0)
					{
						
						f.sel_place.options.length=null;
						f.sel_place.options[0] = new Option('-- Select Stage --', 0);
						for(var i=0; i<arrStage.length; i++) 
							{
								f.sel_place.options[i+1] = new Option(arrStage[i],arrStageID[i]);
							}
						f.sel_place.options[0].selected = true;
					
					} 
				else 
					{
						f.sel_place.options[0] = new Option(">-- Select substage --<",0);
						for(var i=0; i<arrSubstage.length; i++) 
							{
						
							f.sel_place.options[i+1] = new Option(arrSubstage[i],arrSubstageID[i]);
							
							}
						f.sel_place.options[0].selected = true; 
					}
				
				
				}
			
			</script>
			
			

		<form action="" name="doc_edit" method="POST" enctype="multipart/form-data">
					
		 <body onLoad="umple(0);">
			<form action="" name="add_title_form" method="POST">
			<div align="right">
	<table width="90%">
			
						<tr><td width="100px">Nume</td><td><input name="dname" style="width:200px" type="text" value="<? echo $rowdoc['name']; ?>"></td></tr>
						
						<tr><td>Parent type</td><td><select name="sel_type" style="width:205px" onChange="umple(this.selectedIndex)">
																<option value="Stage">Stage</option>
																<option value="Substage">Substage</option></select></td></tr>
						<tr><td>Parent name: </td><td><select name="sel_place" style="width:205px"></select></td></tr>
						
						<tr><td>Acceptance document?</td><td><input type="checkbox" name="acceptance" value="true" checked></td></tr>
						
					  
						<tr><td><input type="submit" name="qr_update_doc" value="Update" ></td>
			<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
			</table>
			</form>
			</body>
		 
		 <?
	}
	else
	{
	?>
	<form action="" name="edit_form" method="POST">
			
			<table width="90%">
				<tr><td width="100px">Nume</td><td><input name="dname" type="text" value="<? echo $rowdoc['name']; ?>" style="width:250px"></td></tr>
				<tr><td><input type="submit" name="qr_update_doc" value="Update" ></td>
			<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
			</table>
			</form>
			
			
	<?
	
	
	}
 }


function delete($idp, $ids, $idss)
{

?>
	<script type="text/javascript">
		
//aici ar trebui sa apara o ferestruica de confirmare dar nu apare!!!!! sare direct la php
	//	var answer = confirm ("You are going to delete a substage! The documents contained in it are going to become general documents! are you sure you want to proceed?");
		if (answer)
		{
			var mutare=0;
			<?php
			if ($idss!=0)
			{
			 $sql="UPDATE `ec_methodology`.`documents` SET `id_stage` = '0' and `id_substage` = '0' WHERE `documents`.`id_stage` = '".$ids."' and `documents`.`id_substage` = '".$idss."' and `documents`.`id_project` = '".$idp."';";
			 mysql_query($sql) or die(mysql_error()); 
			 mysql_query("delete from substages where id = ".$idss.";") or die(mysql_error());
			
			//$result3 = returnQuery("Select * from documents where id_project=".$idp." and id_stage=".$ids." and id_substage=".$idss.";",$dbh);
			/*		$val=mysql_num_rows($result3);
					if (mysql_num_rows($result3) > 0)
					{
						
						echo("mutare=".$val.";");	
						//while ($row3 = mysql_fetch_assoc($result3)) 
						//{;
						//}
					}
					*/
					$val=mysql_num_rows($resultup);
					echo("mutare=".$val.";");
			}
			else
			{
			$sql="UPDATE `ec_methodology`.`documents` SET `id_stage` = '0' and `id_substage` = '0' WHERE `documents`.`id_stage` = '".$ids."' and `documents`.`id_project` = '".$idp."';";
			 mysql_query($sql) or die(mysql_error()); 
			 mysql_query("delete from substages where id_stage = ".$ids.";") or die(mysql_error());
			  mysql_query("delete from stages where id= ".$ids.";") or die(mysql_error());
			 
			}
			
					
			?>
		//	alert('You modified '+mutare+' documents!');
			
		}
		else
		{
		alert ("Oh my God!! Thanks for the notice!");
		}

	</script> 

<?


}

function display_doc($idp, $ids, $idss)
{
	//echo "adaugare doc pt ".$idp." si ".$ids." si ".$idss."\n ";
	$selected="";
	$i = 0; 
	$arrDocs = array(); 
	$arrDocsID = array();
	$arrDocsAcc = array();
	
	$instructiune="SELECT name, id , for_acceptance FROM tip_documente order by name asc;";
	$interogare=mysql_query($instructiune);
	while($row=mysql_fetch_array($interogare))
	{
		$arrDocs[$i] = $row["name"]; 
		$arrDocsID[$i] = $row["id"];
		$arrDocsAcc[$i]=$row["for_acceptance"];
	
		$i++; 
	}
	
	?>
   	<!-- SCRIPT PENTRU POPULAREA COMBOXURILOR si pt checkbox-->
	<script type="text/javascript"> 

		arrDoc = new Array(); 
		arrDocID = new Array();
		arrDocAcc = new Array();
		<? 
		for($i=0;$i<count($arrDocs); $i++){ 
			echo "arrDoc[$i]='".$arrDocs[$i]."';\n"; 
			echo "arrDocID[$i]='".$arrDocsID[$i]."';\n";
			echo "arrDocAcc[$i]='".$arrDocsAcc[$i]."';\n";
		} 
		?>
		
	function fill(i) 
	{
		var f=document.adaugadoc;
			f.sel_name.options.length=null;
		if (i==1)
		{
			f.sel_name.options[0] = new Option('You have already chosen a name',0);
			f.sel_name.options[0].selected = true; 
		}
		else
		{	
			var j=0;		
			//alert(arrDoc.length);
			
				for(var j=0; j<arrDoc.length; j++) 
				{
					f.sel_name.options[j] = new Option(arrDoc[j],arrDoc[j]);
				} 
				f.sel_place.options[0].selected = true;
			
		}
		
//	alert('ff'); 
			
	}
		function check(val) 
	{
		//alert("alerta");
		var f=document.adaugadoc;
		f.acceptance.checked=false;
		f.name_c.value=null;
		//f.sel_place.options.length=null;
	
		if (val==0) 
		{
			f.acceptance.checked=false;
			
		}
		else 
		{
			for(var i=0; i<arrDoc.length; i++) 
			{
				if(arrDoc[i]==val)
				{
					if (arrDocAcc[i]==1) 
					{
						f.acceptance.checked=true;
					} 
					else 
					{
						f.acceptance.checked=false;
					}
				}
			}
			//alert("else");
			
		}
		
	}
	
	</script>
    
    
    <body onLoad="fill(0);">
	<form action="" name="adaugadoc" method="post" >
	<div align="right">
	<table width="90%">
     <tr><td width='100px'>Write name here </td><td width='25%'><input name="name_c" type="text" style="width:200px"  onChange="fill(1);">  </td></tr>
	<tr><td>Or select from: </td><td><select name="sel_name" style="width:205px" onMouseDown="fill(0);" onChange="check(this.value);">
    

  <?
   echo "<option>-- Select Item --</option>\n";
	$instructiune="SELECT name, id , for_acceptance FROM tip_documente order by name asc;";
	$interogare=mysql_query($instructiune);
	while($rand=mysql_fetch_array($interogare))
		{
			if($_GET["sel_name"]==$rand['name']) $selected="selected";
				else $selected="";
			echo("\t   <option {".$selected."} value='".$rand['name']."'> ".$rand['name']." </option>  \n");
			
		} 
	?> 
                                            
                                            
                                            </select></td></tr>
    <tr><td>Acceptance document? </td><td><input type="checkbox" name="acceptance" value="true" checked></td></tr>
    <tr> <td> <input type="submit" name="add_doc" value="Apply" align="right"> </td>
    	<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
    </table>
    </form>
    
	<?
	
 }
 



//display section pentru denumire si parinte subetapa

function display_set($idp)
{

	$selected="";
	//if($x==1)
	//{
	$i = 0; 
	$arrStages = array(); 
	$arrStagesID = array();
	
	$instructiune="SELECT stage_name, id FROM stages WHERE id_project=".$idp." order by stage_order asc;";
	$interogare=mysql_query($instructiune);
	while($row=mysql_fetch_array($interogare))
	{
		$arrStages[$i] = $row["stage_name"]; 
		$arrStagesID[$i] = $row["id"];
		$i++; 
	}
	
	$j = 0;
	$arrSubstagesP=array();
	$arrSubstages = array(); 
	$arrSubstagesID = array();
	
	$instruct="SELECT substage_name, id, id_stage FROM substages WHERE id_stage in( select id from stages where id_project=".$idp.") order by substage_order asc;";
	$interog=mysql_query($instruct);
	while($row=mysql_fetch_array($interog))
	{
		$arrSubstages[$j] = $row["substage_name"]; 
		$arrSubstagesID[$j] = $row["id"];
		$arrSubstagesP[$j]=$row["id_stage"];
		$j++; 
		//echo $j;
	}

	?>
	
	<script type="text/javascript"> 
		arrStage = new Array(); 
		arrStageID = new Array();
		<?php 
		for($i=0;$i<count($arrStages); $i++){ 
			echo "arrStage[$i]='".$arrStages[$i]."';\n"; 
			echo "arrStageID[$i]='".$arrStagesID[$i]."';\n";
		} 
		?>  
		arrSubstage = new Array(); 
		arrSubstageID = new Array(); 
		arrSubstageP = new Array();
		<?php 
		for($i=0;$i<count($arrSubstages); $i++){ 
			echo "arrSubstage[$i]='".$arrSubstages[$i]."';\n"; 
			echo "arrSubstageID[$i]='".$arrSubstagesID[$i]."';\n"; 
			echo "arrSubstageP[$i]='".$arrSubstagesP[$i]."';\n"; 
		} 
		?> 
		

	function umplest(idx) {
	
		var f=document.adaugasubet;
		f.sel_place.options.length=null;
	
		if (idx==0) 
		{
			
			f.sel_place.options.length=null;
			f.sel_place.options[0] = new Option('-- As first Stage --', 0);
			for(var i=0; i<arrStage.length; i++) {
				f.sel_place.options[i+1] = new Option(arrStage[i],arrStageID[i]);
			}
			f.sel_place.options[0].selected = true;
			f.stage_parent.options.length=null;
			f.stage_parent.options[0] = new Option('-- You cannot select parent --', 0);
			
		} 
		else 
		{
			f.stage_parent.options.length=null;
		
			for(var i=0; i<arrStage.length; i++) {
				f.stage_parent.options[i] = new Option(arrStage[i],arrStageID[i]);
			}
			f.stage_parent.options[0].selected = true;
			
		
			var j=1;
			f.sel_place.options[0] = new Option(">-- Place first --<",0);
			for(var i=0; i<arrSubstage.length; i++) 
			{
				if (arrSubstageP[i]==idx) 
				{
					f.sel_place.options[j] = new Option(arrSubstage[i],arrSubstageID[i]);
					j++;
					
				} 
			}
			f.sel_place.options[0].selected = true; 
		}
	}
		
		
		
	function umple(idx) {
	
		var f=document.adaugasubet;
		
		f.sel_place.options.length=null;
			var j=1;
			f.sel_place.options[0] = new Option(">-- Place first --<",0);
			for(var i=0; i<arrSubstage.length; i++) 
			{
				if (arrSubstageP[i]==idx) 
				{
					f.sel_place.options[j] = new Option(arrSubstage[i],arrSubstageID[i]);
					j++;
					
				} 
			}
			f.sel_place.options[0].selected = true; 
		
	} 
	</script>
			
			
	
	<body onLoad="umplest(0);">
	<form action="" name="adaugasubet" method="post" >
    <table width="90%">
	<tr><td width="100px">Type</td><td><select name="sel_type" style="width:225px" onChange="umplest(this.selectedIndex)">
											<option value="Stage">Stage</option>
											<option value="Substage">Substage</option></select></td></tr>
    <tr><td> Name: </td> <td><input name="subname" type="text" value="" style="width:220px"></td></tr>
    <tr><td> Substage parent: </td><td><select name="stage_parent" onChange="umple(this.value)" style="width:225px;"></select></td> </tr>
	<tr><td>Place after</td><td><select name="sel_place" style="width:225px"></select></td></tr>
    <tr> <td>  <input type="submit" name="aplica" value="Apply" align="right"> </td><td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
    </table>
    </form>
  <? 
  }
  




?>